package com.hengxinguotong.zhihuichengjian.widget.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.jingchen.pulltorefresh.Pullable;

/**
 * Created by hjz on 2018/3/20.
 */

public class PullableRecyclerView extends RecyclerView implements Pullable
{
    private static final String TAG = "PullableRecyclerView";

    private LinearLayoutManager layoutManager;
    private View emptyView;

    public PullableRecyclerView(Context context)
    {
        super(context);
    }

    public PullableRecyclerView(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
    }

    public PullableRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        //只能用线性布局
        layoutManager = (LinearLayoutManager) getLayoutManager();
    }

    @Override
    public boolean canPullDown()
    {
        if (layoutManager == null)
        {
            return false;
        }
        if (layoutManager.getItemCount() == 0)
        {
            // 没有item的时候也可以下拉刷新
            return true;
        } else if (layoutManager.findFirstVisibleItemPosition() == 0
                && getChildAt(0).getTop() >= 0)
        {
            // 滑到ListView的顶部了
            return true;
        } else
            return false;
    }

    @Override
    public boolean canPullUp()
    {
        if (layoutManager == null)
        {
            return false;
        }
        int count = layoutManager.getItemCount();
        int firstPosition = layoutManager.findFirstVisibleItemPosition();
        int lastPosition = layoutManager.findLastVisibleItemPosition();
        if (count == 0)
        {
            // 没有item的时候也可以上拉加载
            return true;
        } else if (lastPosition == (count - 1))
        {
            // 滑到底部了
            if (getChildAt(lastPosition - firstPosition) != null
                    && getChildAt(lastPosition - firstPosition).getBottom() <= getMeasuredHeight())
                return true;
        }
        return false;
    }

//    final private AdapterDataObserver observer = new AdapterDataObserver()
//    {
//        @Override
//        public void onChanged()
//        {
//            checkIfEmpty();
//        }
//
//        @Override
//        public void onItemRangeInserted(int positionStart, int itemCount)
//        {
//            checkIfEmpty();
//        }
//
//        @Override
//        public void onItemRangeRemoved(int positionStart, int itemCount)
//        {
//            checkIfEmpty();
//        }
//    };
//
//    private void checkIfEmpty()
//    {
//        if (emptyView != null && getAdapter() != null)
//        {
//            final boolean emptyViewVisible =
//                    getAdapter().getItemCount() == 0;
//            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
//            setVisibility(emptyViewVisible ? GONE : VISIBLE);
//        }
//    }
//
//    @Override
//    public void setAdapter(Adapter adapter)
//    {
//        final Adapter oldAdapter = getAdapter();
//        if (oldAdapter != null)
//        {
//            oldAdapter.unregisterAdapterDataObserver(observer);
//        }
//        super.setAdapter(adapter);
//        if (adapter != null)
//        {
//            adapter.registerAdapterDataObserver(observer);
//        }
//
//        checkIfEmpty();
//    }
//
//    public void setEmptyView(View emptyView)
//    {
//        this.emptyView = emptyView;
//        checkIfEmpty();
//    }
}
